/*
 * Copyright 1997-2008 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 *   - Neither the name of Sun Microsystems nor the names of its
 *     contributors may be used to endorse or promote products derived
 *     from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.io.*;
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;

import javax.mail.*;
import javax.mail.internet.*;

import com.sun.mail.smtp.*;

public class smtpsend {
    public static void main(String[] argv) {
  
	String  to="**********@gmail.com"; // Reciepient Address(To)
	String subject = "******TEST MAIL****"; // Subject of mail
	String from = "**********@gmail.com"; // Sender Address (From) 
	String cc = null, bcc = null, url = null;
	String mailhost = "smtp.gmail.com";
	String mailer = "smtpsend";
	String file = null;
	String protocol = null, host = null;
	String user = "**********"; // Sender's username of gmail Account
	String password = "**********"; // Sender's password of gmail Account
	boolean debug = false;
	boolean verbose = false;
	boolean auth = true;
	String prot = "smtp";

	try {
	    /*
	     * Initialize the JavaMail Session.
	     */
//		java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
	    Properties props = System.getProperties();
	    if (mailhost != null)
		props.put("mail." + prot + ".host", mailhost);
	    if (auth)
		props.put("mail." + prot + ".auth", "true");
	    props.put("mail.smtp.starttls.enable", "true");
	    Session session = Session.getInstance(props, null);
	    if (debug)
		session.setDebug(true);
	    /*
	     * Construct the message and send it.
	     */
	    Message msg = new MimeMessage(session);
	 
	    if (from != null)
		msg.setFrom(new InternetAddress(from));
	    else
		msg.setFrom();

	    msg.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(to, false));
	    if (cc != null)
		msg.setRecipients(Message.RecipientType.CC,
					InternetAddress.parse(cc, false));
	    if (bcc != null)
		msg.setRecipients(Message.RecipientType.BCC,
					InternetAddress.parse(bcc, false));

	    msg.setSubject(subject);
	    String text = "Message of the mail....";
		msg.setText(text);
	    msg.setHeader("X-Mailer", mailer);
	    msg.setSentDate(new Date());
	    SMTPTransport t =
		(SMTPTransport)session.getTransport(prot);
	    try {
		if (auth)
		    t.connect(mailhost, user, password);
		else
		    t.connect();
		msg.saveChanges();
		t.sendMessage(msg, msg.getAllRecipients());
		
	    }catch(Exception exx)
	    {
	    	System.out.println("Ex  is:"+exx.getMessage());
	    	exx.printStackTrace();
	    }
	    
	    finally {
		if (verbose)
		    System.out.println("Response: " +
						t.getLastServerResponse());
		t.close();
	    }

	    System.out.println("\nMail was sent successfully.");

    }catch(Exception ex)
    {
    	System.out.println("Exception is:"+ex.getMessage());
    	ex.printStackTrace();
    }
	finally
	{
		System.out.println("Statement in finally block...");
	}
}
}
